package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.StartService;
import cn.web.tunnelrobot.dao.HardwareTestInfoDao;
import cn.web.tunnelrobot.dao.UserRecordDao;
import cn.web.tunnelrobot.dto.UserRecordDTO;
import cn.web.tunnelrobot.pojo.User;
import cn.web.tunnelrobot.pojo.UserRecord;
import cn.web.tunnelrobot.service.UserRecordService;
import cn.web.tunnelrobot.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 用户使用记录service层
 */
@Service
public class UserRecordServiceImpl  implements UserRecordService {
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private HardwareTestInfoDao hardwareTestInfoDao;
    //获取用户session信息
    //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    //User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
    //log4j
    Logger logger = LoggerFactory.getLogger(UserRecordServiceImpl.class);

    @Override
    public HashMap<String, Object> getUserRecordList(UserRecordDTO userRecordDTO) {
        HashMap<String, Object> map = new HashMap<>();
        /*StringBuilder sbu = new StringBuilder();
        if(!StringUtils.isEmpty(userRecordDTO.getUsName())){
            sbu.append(",用户姓名:"+userRecordDTO.getUsName());
        }
        if(!StringUtils.isEmpty(userRecordDTO.getUrOperatContent())) {
            sbu.append(",操作内容:"+userRecordDTO.getUrOperatContent());
        }
        if(userRecordDTO.getUrOperatTime() != null){
            sbu.append(",操作时间:"+userRecordDTO.getUrOperatTime());
        }
        UserRecord userRecord = new UserRecord();
        if(!StringUtils.isEmpty(sbu.toString())){
            userRecord.setUrOperatContent("查询用户日志列表");
        }else{
            userRecord.setUrOperatContent("查询用户日志列表,携带条件："+sbu.toString());
        }
        userRecord.setUsId(user.getUsId());
        userRecord.setUrOperatTime(new Date());
        userRecordDao.addUserRecord(userRecord);*/
        try {
            List<UserRecordDTO> userRecordList = userRecordDao.getUserRecordList(userRecordDTO);
            if(userRecordList.size() > 0){
                map.put("code",0);
                map.put("data",userRecordList);
                int count = userRecordDao.getUserRecordListCount(userRecordDTO);
                if(count > 0){
                    map.put("count",count);
                }else{
                    map.put("count",0);
                }
                return map;
            }else{
                map.put("code",-1);
                map.put("data","查询暂无信息");
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","查询信息时出错");
            return map;
        }
    }

    @Override
    public HashMap<String, Object> getUserRecordListYearAgo() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            List<UserRecordDTO> userRecordList = userRecordDao.getUserRecordListYearAgo();
            if(userRecordList.size() > 0){
                System.out.println("一年数据之前最大的urID:"+userRecordList.get(0).getUrId());
                userRecordDao.deleteUserRecordList(userRecordList.get(0).getUrId());
            }
           /* UserRecord userRecord = new UserRecord();
            userRecord.setUsId(1);
            userRecord.setUrOperatContent("(系统运行期间自动执行)删除一年之前的日志记录");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","查询信息时出错");
            return map;
        }
        return map;
    }

    @Override
    public HashMap<String, Object> InfoStatistical(String operStartTime,String operEndTime) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            if(operStartTime == null && operEndTime != null){
                //开始时间设置为今天0点
                Date date = new Date();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd 00:00:00");
                operStartTime = dateFormat.format(date);
            }
            if(operStartTime != null && operEndTime == null){
                //结束时间设置为今天24点
                Date date = new Date();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd 23:59:59");
                operEndTime = dateFormat.format(date);
            }

            List<UserRecordDTO> getList = userRecordDao.getControlRobotInfo(operStartTime,operEndTime);
            Integer sideStationNumber = 0;//旁站次数
            Integer sideStationTime = 0;//旁站时长
            if(getList != null && getList.size() > 0){
                for (int i = 0 ;i<getList.size(); i++){
                    if((i+1) < getList.size()){
                        UserRecordDTO userRecordDTO1 = getList.get(i);
                        UserRecordDTO userRecordDTO2 = getList.get(i+1);
                        long from1 = userRecordDTO1.getUrOperatTime().getTime();
                        long from2 = userRecordDTO2.getUrOperatTime().getTime();
                        int hours = (int) ((from2 - from1) / (1000 * 60));
                        logger.info("两个时间之间的分钟差为：" + hours);
                        if(hours < 120){
                            sideStationNumber++;
                            sideStationTime=sideStationTime+hours;
                        }
                    }
                }
            }
            //旁站次数
            map.put("sideNumber",sideStationNumber);
            //旁站时长
            map.put("sideLength",sideStationTime);
            //巡视次数=>车体
            map.put("inspectNumber",userRecordDao.getInspectCount(operStartTime,operEndTime));
            //验收次数
            map.put("acceptanceNumber",hardwareTestInfoDao.getInformationInputCount(operStartTime,operEndTime));
            map.put("code",0);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","查询信息时出错");
        }
        return map;
    }
    /**
     *gbq
     *操作记录添加
     * */
    @Override
    public void addUserRecord(UserRecord userRecord) {
        try {
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


       /* @Override
    public HashMap<String, Object> addUserRecord(UserRecord userRecord) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(usId);
            userRecord.setUrOperatContent(urOperatContent);
            userRecord.setUrOperatTime(new Date());
            int result = userRecordDao.addUserRecord(userRecord);
            if(result > 0){
                map.put("code",0);
                map.put("msg","添加成功");
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","添加用户使用记录时出错");
            return map;
        }
    }*/

}
